<template>
  <section class="icons">
    <div v-for="(item, index) in list" :key="index" class="icons-icon" :class="{'active': item.id == value}" @click="select(item)">
      <icon :name="item.icon" />
    </div>
  </section>
</template>
<script>
import mixins from './../mixins'
import icons from '@/utils/icons'
export default {
  name: 'Icons',
  mixins: [mixins],
  props: {
    option: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      list: Object.keys(icons).map(a => {
        return { id: a, icon: a }
      }),
      icons: icons
    }
  },
  watch: {
    value: {
      handler (v) {
        this.check()
        this.dispatch_event('filed-value-change', v)
      },
      deep: true
    }
  },
  methods: {
    select ({ id }) {
      this.value = id
    },

    setValue (v) {
      this.value = v
      this.check()
    },

    getValue () {
      return this.value
    },

    check () {
      const { value, required } = this
      if (!value && required) {
        this.error_ = true
        this.error_message_ = '必填选项不能为空'
      } else {
        this.error_ = false
      }
      this.fieldCheck()
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/var.scss';
.icons {
  &-icon {
    font-size: 20px;
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    vertical-align: top;
    box-sizing: border-box;
    border: 1px solid $f6;
    border-radius: 3px;
    text-align: center;
    margin: 0 4px 4px 0;
    &.active {
      color: $a1;
      border: 1px solid $a1;
    }
  }
}
</style>
